package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.domain.tenant.entity.HmOperationLog;
import com.harmontronics.hanma.paas.paas.dto.output.HmOperationLogResult;
import com.harmontronics.hanma.paas.paas.service.HmOperationLogAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;


/**
 * 操作日志
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-27 10:51:35
 */
@RestController
@RequestMapping("/api/hm-operation-log")
@Api(tags = {"操作日志"})
public class HmOperationLogController {
    @Autowired
    private HmOperationLogAppService hmOperationLogappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmOperationLogResult>> list() {
        List<HmOperationLog> hmOperationLogList = hmOperationLogappService.getList();

        return ResponseEntity.ok(Convert.toList(HmOperationLogResult.class, hmOperationLogList));
    }

    /**
     * 分页列表
     */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmOperationLogResult>> list(@ApiParam("提交人") @RequestParam(required = false) String submitter,
                                                               @ApiParam("开始时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") LocalDateTime startTime,
                                                               @ApiParam("结束时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") LocalDateTime endTime,
                                                               @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                                               @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmOperationLog> hmOperationLogPageBase = hmOperationLogappService.getList(submitter, startTime, endTime, pageNo, pageSize);
        PageBase<HmOperationLogResult> resultPageBase = PageBase.copy(hmOperationLogPageBase, HmOperationLogResult.class);
        return ResponseEntity.ok(resultPageBase);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmOperationLogResult> info(@RequestParam("id") Long id) {
        HmOperationLog hmOperationLog = hmOperationLogappService.findById(id);
        HmOperationLogResult result = BeanUtil.toBean(hmOperationLog, HmOperationLogResult.class);
        return ResponseEntity.ok(result);
    }


    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmOperationLogappService.remove(Arrays.asList(ids));

        return ResponseEntity.ok();
    }

}
